package com.example.epidemic_assistant.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.epidemic_assistant.gen.DaoSession;
import com.android.epidemic_assistant.gen.UserBeanDao;
import com.example.epidemic_assistant.App;
import com.example.epidemic_assistant.R;
import com.example.epidemic_assistant.bean.UserBean;
import com.example.epidemic_assistant.ui.UserManagerDetailsActivity;
import com.example.epidemic_assistant.ui.adapter.TemperatureAdapter;
import com.example.epidemic_assistant.ui.adapter.UserManagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理
 */
public class UserManagerFragment extends Fragment implements UserManagerAdapter.IOnItemClick {

    private final static String TAG = "UserManagerFragment";

    private RecyclerView mRvUser;

    private UserManagerAdapter mUserManagerAdapter;

    private App mMyApp;
    private DaoSession mDaoSession;
    private UserBeanDao mUserBeanDao;
    private List<UserBean> mUserBeans;

    private List<UserBean> mUserBeanList;
    private List<Integer> mRoomIdList;

    public static UserManagerFragment newInstance() {
        return new UserManagerFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_user_manager, container, false);

        initView(view);
        initData();
        initListener();

        return view;
    }

    private void initListener() {

    }

    private void initData() {
        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mUserBeanDao = mDaoSession.getUserBeanDao();
        mUserBeans = mUserBeanDao.loadAll();

        mUserBeanList = new ArrayList<>();
        mRoomIdList = new ArrayList<>();
        for (UserBean userBean : mUserBeans) {
            if (!userBean.isAdmin() && !mRoomIdList.contains(userBean.getRoomId())) {
                mUserBeanList.add(userBean);
                mRoomIdList.add(userBean.getRoomId());
            }
        }
        mRvUser.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        mUserManagerAdapter = new UserManagerAdapter(requireContext(), mUserBeanList);
        mUserManagerAdapter.setIOnItemClick(this);
        mRvUser.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));
        mRvUser.setAdapter(mUserManagerAdapter);
    }

    private void initView(View view) {
        mRvUser = view.findViewById(R.id.rv_user);
    }

    @Override
    public void adapterItemClick(int position) {
        Intent intent = new Intent(requireContext(),UserManagerDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("position",position);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}